/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sistemperpustakaan.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import sistemperpustakaan.entity.DendaBuku;
import sistemperpustakaan.utility.DbUtility;
import sistemperpustakaan.view.denda.FrmDendaBuku;

/**
 *
 * @author EKo
 */
public class DendaModel {

    Connection connection;
    FrmDendaBuku frmDendaBuku;
    DendaBuku dendaBuku;

    public DendaModel() {
        this.connection = new DbUtility().getConnection();
    }

    public DendaBuku getViewDendaBuku() {
        String query = "SELECT * FROM denda_buku";
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(query);
//            statement.setString(1, UserId);
            ResultSet rs = statement.executeQuery();
            if (rs.next()) {
                dendaBuku = new DendaBuku(rs.getInt(1));
            }
//            System.out.println(query);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Error when insert data \n Error: " + e.getMessage());
            try {
                connection.rollback();
            } catch (Exception ex) {
            }
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (Exception e) {
            }
        }
        return dendaBuku;
    }

    public boolean updateDendaBuku(int denda) {
        PreparedStatement statement = null;
        try {
            connection.setAutoCommit(false);
            statement = connection.prepareStatement("UPDATE denda_buku SET denda="+denda+"");
            if (statement.executeUpdate() == 1) {
                connection.commit();
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error when update  data \n Error: " + e.getMessage());
            try {
                connection.rollback();
            } catch (Exception ex) {
            }
            return false;
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (Exception e) {
            }
        }
        return true;
    }
}
